package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleParseSynoym implements ParseSynoym{

	Map<String, List<String>> synoyMap = new HashMap<String, List<String>>();
	
	@Override
	public void loadSynoym(File file) {
		BufferedReader br = null;
		String sCurrentLine = "";
		try {
			br = new BufferedReader(new FileReader(file));
			while((sCurrentLine = br.readLine()) != null){
				String[] split = sCurrentLine.split(",");
				List<String> list = new ArrayList<String>();
				if(split.length > 1){
					for(int i = 1;i < split.length;i++){
						list.add(split[i]);
					}
				}
				synoyMap.put(split[0], list);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	@Override
	public String parseString(String str) {
		
		for(String mkey : synoyMap.keySet()){
			for(String lkey:synoyMap.get(mkey)){
				str = str.replaceAll(lkey, mkey);
			}
		}
		
		return str;
	}

}
